package com.myopen.converter;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Record;
import com.myopen.cachestatic.DicCacheStatic;

public class ObjectConverter<T> {

	public String[] dicTypeKeys;//字段名称
	public String[] dicTypes;//字典名称，数量必须与dicTypeKeys一样
	public String[] mapTypes;
	public T t;
	
	public ObjectConverter(T t,String[] dicTypeKeys,String[] dicTypes,String[] mapTypes) {
		this.t=t;
		this.dicTypeKeys=dicTypeKeys;
		this.dicTypes=dicTypes;
		this.mapTypes=mapTypes;
		
	}
	
	@SuppressWarnings("unchecked")
	public T getObject() {
		Class<? extends Object> clazz=t.getClass();
		Method mGet=null;
		try {
			mGet = clazz.getMethod("getStr",String.class);
		} catch (NoSuchMethodException e1) {
			e1.printStackTrace();
		} catch (SecurityException e1) {
			e1.printStackTrace();
		}
		
		if(dicTypeKeys!=null&&dicTypes!=null) {
			for (int i=0;i<dicTypeKeys.length;i++) {
				
				try {
					String dicInfoCode=(String)mGet.invoke(t, dicTypeKeys[i]);
					if(!StrKit.isBlank(dicInfoCode)) {
						
						Record dicInfo=DicCacheStatic.getDicInfo(dicTypes[i], dicInfoCode);
						if(dicInfo==null) {
							dicInfo=new Record();
						}
						if(t instanceof Model<?>) {
							//如果是Model要使用put方法
							Method mPet=clazz.getMethod("put",String.class,Object.class);
							mPet.invoke(t, dicTypeKeys[i ]+"_dic",dicInfo.getStr("name"));
						}else {
							//如果是Record要使用set方法
							Method mSet=clazz.getMethod("set",String.class,Object.class);
							mSet.invoke(t, dicTypeKeys[i]+"_dic",dicInfo.getStr("name"));
						}
						
					}
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				} catch (IllegalArgumentException e) {
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					e.printStackTrace();
				} catch (NoSuchMethodException e) {
					e.printStackTrace();
				} catch (SecurityException e) {
					e.printStackTrace();
				}
			}
		}
		
		if(mapTypes!=null) {
			for(int i=0;i<mapTypes.length;i++) {
				
				try {
					String jsonString=(String)mGet.invoke(t, mapTypes[i]);
					if(!StrKit.isBlank(jsonString)) {
						
						Map<String,Object> map=(Map<String,Object>)JSON.parse(jsonString);
						
						if(t instanceof Model<?>) {
							//如果是Model要使用put方法
							Method mPet=clazz.getMethod("put",String.class,Object.class);
							mPet.invoke(t, mapTypes[i ]+"_map",map);
						}else {
							//如果是Record要使用set方法
							Method mSet=clazz.getMethod("set",String.class,Object.class);
							mSet.invoke(t, mapTypes[i]+"_map",map);
						}
						
					}
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				} catch (IllegalArgumentException e) {
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					e.printStackTrace();
				} catch (NoSuchMethodException e) {
					e.printStackTrace();
				} catch (SecurityException e) {
					e.printStackTrace();
				}
			}
		}
		
		return t;
		
	}
}
